﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace FySystem.Extension
{
    public static class DateTimeExtension
    {
        private static CultureInfo _defaultCulture = new CultureInfo("zh-cn");

        /// <summary>
        /// 获取日开始时间，如：2020-11-02 00:00:00
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string DayStartTime(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd 00:00:00");
        }

        /// <summary>
        /// 获取日开始时间，如：2020-11-02 00:00:00
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime DayStartTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-MM-dd 00:00:00"), _defaultCulture);
        }

        /// <summary>
        /// 获取日结束时间：如：2020-11-02 23:59:59
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string DayEndTime(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd 23:59:59");
        }

        /// <summary>
        /// 获取日结束时间：如：2020-11-02 23:59:59
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime DayEndTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-MM-dd 23:59:59"), _defaultCulture);
        }

        /// <summary>
        /// 获取月开始时间：如：2020-11-01 00:00:00
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string MonthStartTime(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-01 00:00:00");
        }

        /// <summary>
        /// 获取月开始时间：如：2020-11-01 00:00:00
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime MonthStartTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-MM-01 00:00:00"), _defaultCulture);
        }

        /// <summary>
        /// 获取月结束时间：如：2020-11-30 23:59:59
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string MonthEndTime(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-MM-01")).AddMonths(1).AddSeconds(-1).ToString("yyyy-MM-dd 23:59:59");
        }

        /// <summary>
        /// 获取月结束时间：如：2020-11-30 23:59:59
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime MonthEndTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-MM-01")).AddMonths(1).AddMilliseconds(-1);
        }

        /// <summary>
        /// 获取年开始时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string YearStartTime(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-01-01 00:00:00");
        }

        /// <summary>
        /// 获取年开始时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime YearStartTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-01-01 00:00:00"));
        }

        /// <summary>
        /// 获取年结束时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string YearEndTime(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-12-31 23:59:59");
        }

        /// <summary>
        /// 获取年结束时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime YearEndTimeD(this DateTime dateTime)
        {
            return DateTime.Parse(dateTime.ToString("yyyy-12-31 23:59:59"));
        }
    }
}
